﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>管理员登录</title>
    <link rel="stylesheet" href="/css/admin.css">
</head>
<body>
    <div class="layer">
        <div>
            <div class="i" id=""></div>
        </div>
    </div>
    <!-- 登录 -->
    <div class="box">
        <div class="box1">
            <h3 class="logo">欢迎登录</h3>
            @* action="/ManageUsers" method="post" *@
            <form >
                <table align="center">
                    <tr>
                        <td>账号</td>
                        <td><input type="text" id="admins" class="input"></td>
                    </tr>
                    <tr>
                        <td>密码</td>
                        <td><input type="password" id="password" class="input"></td>
                    </tr>
                </table>
                <p align="center"><input type="button" value="登录" class="submit"></p><p class="zhuce" align="center">
                    <a href="~/login">用户</a>
                    <a href="~/aregister">注册</a>
                </p>
            </form>
        </div>
    </div>
</body>
</html>
<script src="~/js/jquery-3.6.0.min.js"></script>
<script>
    const inputs = document.querySelectorAll(".input")

    function addcl() {
        let parent = this.parentNode.parentNode;
        parent.classList.add("focus")
    }

    function remcl() {
        let parent = this.parentNode.parentNode;
        if (this.value === "") {
            parent.classList.remove("focus")
        }
    }

    inputs.forEach(input => {
        input.addEventListener("focus", addcl);
        input.addEventListener("blur", remcl);
    })

    // dom操作
    // 选择元素
    var layer = $('.layer');
    var layer_info = $('.layer div div');
    // 登录
    $('.submit').click(function () {
        var info = $('form input');
        $.ajax({
            type: 'POST',
            url: './api/users/login',
            data: {
                "username": info[0].value,
                "password": info[1].value
            },
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.status == "fail") {
                    alert("账号或密码错误");
                    return null;
                }
                alert('登录成功')
                // 保存username
                localStorage.setItem("AdminName", data.data)
                setTimeout(function () { window.location.href = '/ManageUsers'; }, 1500);
            },
            error: function (err) {
                console.log(err.responseText);
                alert("服务器错误");
            }
        });
    });
</script>